package com.zkk.service.impl;

import com.zkk.dao.UserDao;
import com.zkk.dao.impl.UserDaoImpl;
import com.zkk.pojo.User;
import com.zkk.service.UserService;
import com.zkk.utils.PageBeanUtils;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();
    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    public User login(String username, String password) {
        return userDao.login(username,password);
    }

    @Override
    public int insert(User user) {
        return userDao.insert(user);
    }

    @Override
    public User findUserById(int id) {
        return userDao.findUserById(id);
    }

    @Override
    public int deleteById(int id) {
        return userDao.deleteById(id);
    }

    //实现分页的业务逻辑


    @Override
    public PageBeanUtils<User> findAll(PageBeanUtils<User> pageBeanUtils) {
        int currentPage = pageBeanUtils.getCurrentPage();
        int pageSize = pageBeanUtils.getPageSize();
        List<User> users = userDao.findAll((currentPage - 1) * pageSize, pageSize);
        //当前页数据
        pageBeanUtils.setPageData(users);
        //获得总记录数
        int count = userDao.count();
        pageBeanUtils.setTotalData(count);
        //获得总页数
        pageBeanUtils.setTotalPage((count+pageSize-1)/pageSize);
        //返回通过上面调用到的方法得到的pageBeanUtils
        return pageBeanUtils;
    }
}
